<?php
/** UnivIS update script
 * Fetches content from UnivIS via PRG interface in XML and stores 
 * it into a mysql database
 */

require_once 'conf.php';

require_once 'modules/generic_module.php';
require_once 'modules/departments_module.php';
require_once 'modules/lectures_module.php';


// Connect to database
$dbConn = mysql_pconnect($conf['db']['host'], $conf['db']['user'], $conf['db']['pass']);
if (!$dbConn) {
	die("\nError connecting to database at '{$conf['db']['host']}'.\n");
}
if (!mysql_select_db($conf['db']['db'], $dbConn)) {
	mysql_close($dbConn);
	die("\nError selecting database '{$conf['db']['db']}'.\n");
}

// Import modules
foreach ($conf['univis']['modules'] as $module => $import)
{
    if (false === $import)
    {
	continue;
    }

    $moduleClass = ucfirst($module).'Module';
    if (!class_exists($moduleClass))
    {
	$moduleClass = 'GenericModule';
    }
    $moduleController = new $moduleClass($module); 

    do
    {
	$loop = false;
	$url = $moduleController->getUrl(&$conf['univis']);

	$rawxml = '';
	$xml = '';
	print '> Fetching '.ucfirst($module)." XML data from\n"
		."    {$url}\n";
	ob_start();
	readfile($url);
	$rawxml = ob_get_contents();
	ob_end_clean();
	$xml = str_replace(array('&#x0A;', '&#x0D;'), array("\n", "\r"), $rawxml);
	unset($rawxml);

	// Load parser
	$parserClass = 'Parser';
	include_once 'parsers/parser.php';
	$parser = new $parserClass($module);
	$parser->parse($xml);
	print '< Parsing of '.ucfirst($module)." done.\n";

	$loop = $moduleController->hasMoreUrls();
    } while($loop);
}

// Close database connection
mysql_close($dbConn);
?>